<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>客户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/public.css" media="all"/>
</head>
<body class="childrenBody">
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form layui-form-pane">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">学号</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" placeholder="姓名" id="xhQuery">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" placeholder="手机号" id="xmQuery">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn" id="searchBtn" type="button">搜索</button>
                    <button class="layui-btn layui-btn-primary" type="reset">重置</button>
                </div>
            </div>
        </form>
    </blockquote>
    <table id="dataTable" lay-filter="dataTableFilter"></table>
    <!--操作-->
    <script type="text/html" id="headBtns">
        <button class="layui-btn layui-btn-sm" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
        <%--        <shiro:hasPermission name="customer:export">--%>
        <%--            <button class="layui-btn layui-btn-sm" lay-event="export">--%>
        <%--                <i class="layui-icon layui-icon-release"></i>--%>
        <%--                导出--%>
        <%--            </button>--%>
        <%--        </shiro:hasPermission>--%>
        <%--        <shiro:hasPermission name="customer:import">--%>
        <%--            <button class="layui-btn layui-btn-sm" lay-event="import">--%>
        <%--                <i class="layui-icon layui-icon-upload"></i>--%>
        <%--                导入--%>
        <%--            </button>--%>
        <%--        </shiro:hasPermission>--%>
    </script>
    <script type="text/html" id="rowBtns">
        <button class="layui-btn layui-btn-sm" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>
            修改
        </button>
        <button class="layui-btn layui-btn-sm" lay-event="del">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
        <%--        <shiro:hasPermission name="customer:update">--%>
        <%--            <button class="layui-btn layui-btn-sm" lay-event="edit">--%>
        <%--                <i class="layui-icon layui-icon-edit"></i>--%>
        <%--                修改--%>
        <%--            </button>--%>
        <%--        </shiro:hasPermission>--%>
    </script>
</form>
<%-- 编辑客户的模板 --%>
<script type="text/html" id="editTpl">
    <form class="layui-form layui-form-pane" style="margin: 10px" lay-filter="editFormFilter">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">学号</label>
                <div class="layui-input-inline">
                    <input id="xh" type="text" name="xh" lay-verify="required" lay-reqText="请输入学号" placeholder="学生学号"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="xm" lay-verify="required" lay-reqText="请输入姓名" placeholder="姓名"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-inline">
                    <input type="radio" name="xb" value="1" title="男" checked>
                    <input type="radio" name="xb" value="0" title="女">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">出生年月</label>
                <div class="layui-input-inline">
                    <input type="text" id="test1" name="cssj" lay-reqText="请输入出生年月" placeholder="出生年月"
                           class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">专业id</label>
                <div class="layui-input-inline">
                    <input type="text" name="zyId" lay-verify="required" lay-reqText="请输入专业id" placeholder="专业id"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">总学分</label>
                <div class="layui-input-inline">
                    <input type="text" name="zxf" lay-reqText="请输入总学分" placeholder="总学分" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="bz" placeholder="备注" lay-reqText="请输入备注"
                          lay-verify="required"></textarea>
            </div>
        </div>
        <button type="button" style="display: none" id="subBtn" lay-submit lay-filter="subBtnFilter"></button>
    </form>
</script>
<%-- 上传文件模板 --%>
<script type="text/html" id="uploadTpl">
    <form class="layui-form layui-form-pane" style="padding-left: 70px; padding-top:30px ">
        <div class="layui-form-item">
            <label class="layui-form-label">客户文件</label>
            <div class="layui-inline">
                <button type="button" class="layui-btn layui-btn-normal" id="chooseBtn">选择文件</button>
                <button type="button" class="layui-btn" id="uploadBtn">上传</button>
            </div>
        </div>
    </form>
</script>

<script type="text/javascript" src="${pageContext.request.contextPath}/resources/layui/layui.js"></script>
<script>
    layui.use(['table', 'jquery', 'layer', 'form', 'upload', 'laydate'], function () {
        let cxt = '${pageContext.request.contextPath}';
        let table = layui.table;
        let $ = layui.jquery;
        let layer = layui.layer;
        let form = layui.form;
        let upload = layui.upload;
        let laydate = layui.laydate;


        //渲染数据表格
        //表格参数
        let tabOps = {
            id: "dataTableId",
            elem: "#dataTable",
            url: cxt + "/user/getList",
            page: true,//开启分页
            toolbar: "#headBtns",//头工具栏
            cols: [[
                {type: "checkbox"},
                {field: "xh", align: "center", title: "学号"},
                {field: "xm", align: "center", title: "姓名"},
                {
                    title: "性别", width: 80, templet: function (d) {
                        let sex = d.xb;
                        if (sex == 1) {
                            return "<p style='color: #1385e8'>男</p>";
                        } else if (sex == 0) {
                            return "<p style='color: #e8139a'>女</p>";
                        } else {
                            return "<p style='color: #e51111'>不详</p>";
                        }
                    }
                },
                {
                    title: "出生时间", minWidth: 160, templet: function (d) {
                        let time = new Date(d.cssj);
                        return time.toLocaleString();

                    }
                },
                {field: "zyId", align: "center", title: "专业id"},
                {field: "bz", align: "center", title: "备注", width: 120},
                {
                    title: "", align: "center", width: 80, templet: function (d) {
                        let img = d.zp;
                        return "<button class='layui-btn layui-btn-xs' onclick=showUserImg(\'" + img + "\')>查看</button>"
                    }
                },
                {title: "操作", align: "center", toolbar: "#rowBtns", minWidth: 230, fixed: "right"}
            ]],//列数据
            parseData: function (rs) {
                return {
                    code: rs.code,
                    msg: rs.msg,
                    count: rs.total,
                    data: rs.data
                }
            },
            response: {
                statusCode: 200
            }
        };
        //进行渲染
        let tabIns = table.render(tabOps);
        //按钮查询
        $("#searchBtn").click(function () {
            let xh = $("#xhQuery").val();
            let xm = $("#xmQuery").val();
            tabIns.reload({
                where: {
                    "xh": xh,
                    "xm": xm,
                }
            })
        });

        //表格头工具栏监听事件
        table.on("toolbar(dataTableFilter)", function (d) {
            let event = d.event;
            if (event == "add") {
                add();
            }
        })

        //展示图片的地方

        window.showUserImg = function (url) {
            let imgData = {
                "title": "用户头像", //相册标题
                "start": 0, //初始显示的图片序号，默认0
                "data": [   //相册包含的图片，数组格式
                    {
                        "alt": "用户头像",
                        "src": cxt + "/" + url, //原图地址
                        "thumb": url //缩略图地址
                    }
                ]
            }
            layer.photos({
                photos: imgData
            });
        };

        //弹出层的参数
        let layOps = {
            title: "",
            skin: "layui-layer-molv",
            type: 1,
            content: $("#editTpl").html(),
            area: ['425px', '530px'],
            success: function (layero, index) {
                form.render("radio");
                //表单的提交监听
                form.on("submit(subBtnFilter)", function (d) {
                    let formData = d.field;
                    //使用ajax提交数据
                    $.post(cxt + "/user/addUser", formData, function (rs) {
                        layer.msg(rs.msg);//展示业务消息
                        if (rs.code != 200) {
                            return false;
                        }
                        layer.close(index);//关闭弹层
                        //刷新表格
                        $("#searchBtn").click();
                    })
                    return false;//阻止默认提交行为
                })
            },
            btn: ['确认', '取消'],
            btnAlign: "c",
            yes: function (index, layero) {
                //点击隐藏的提交按钮  触发 表单提交监听
                $("#subBtn").click();
                title = "";
            }
        };

        /**
         * 新增学生的方法
         */
        function add() {
            layOps.title = "新增用户信息";
            layer.open(layOps);
            laydate.render({
                elem: '#test1',//指定元素
                type: 'datetime'
            });
        }

        //行工具栏监听事件
        table.on("tool(dataTableFilter)", function (d) {
            let event = d.event;
            let rowData = d.data;
            if (event == "edit") {
                edit(rowData);//修改客户
            } else if (event = "del") {
                console.log('删除了')
                deleteRowData(rowData)
            }
        });

        //具体的修改方法
        function edit(rowData) {
            //将对象复制一份
            let ops = new Object();//目标对象
            layOps.title = "修改用户信息";
            Object.assign(ops, layOps);//将layOps 对象复制给 ops 对象
            ops.success = function (layero, index) {
                form.render("radio");
                form.val("editFormFilter", rowData); // 进行数据回显
                //只能允许这个地方的数据进行只读
                //表单的提交监听
                form.on("submit(subBtnFilter)", function (d) {
                    let formData = d.field;
                    formData.id = rowData.id;//获取ID
                    //使用ajax提交数据
                    $.post(cxt + "/user/updateUser", formData, function (rs) {
                        layer.msg(rs.msg);//展示业务消息
                        if (rs.code != 200) {
                            return false;
                        }
                        layer.close(index);//关闭弹层
                        //刷新表格
                        $("#searchBtn").click();
                    })
                    return false;//阻止默认提交行为
                })
            }
            layer.open(ops);
            laydate.render({
                elem: '#test1',//指定元素
                type: 'datetime'
            });
            $("#xh").attr("readonly", "true");      //添加这个不可修改的操作
        }

        //具体的删除的方法
        function deleteRowData(rowData) {
            layer.confirm('是否删除学生信息', {
                btn: ['确定', '取消'] //按钮
            }, () => {
                let formData = {};
                formData.xh = rowData.xh;//获取ID
                //使用ajax提交数据
                $.post(cxt + "/user/deleteUser", formData, function (rs) {

                    layer.msg(rs.msg);//展示业务消息
                    if (rs.code != 200) {
                        return false;
                    }
                    //刷新表格
                    $("#searchBtn").click();
                })
                return false;//阻止默认提交行为
            }, () => {
            });
        }
    })


</script>
</body>
</html>